var QUERY_KEY;
var QUERY_ACTION;
var _paramMap={};
var _PA_NM = "cia.console.";
var queryResult;
var radioClick=false;
var translateArray=new Array();
var _transFlag=new Array();
var transResultArray=new Array();
var loadFinished=false;//列表是否加载完全
var queryFlag=true;
var reloadFlag=false;
var i=0*1;
function reload(){
	reloadFlag = true;
	var queryKey= window.parent.queryKey;
	if(queryKey == ""){
		reQuery();
	}
	else{
		resetQueryDiv(queryKey);
		reQuery();
	}
}
function showQueryResult(){
	if(document.getElementById("queryResult") !=null)
		document.getElementById("queryResult").style.display="block";
}
function reQuery()
{
	try{
		//transRowDataValue();
		var paramMap = queryPre();		
		if(paramMap)
		{
			_paramMap =paramMap;			
			loadFinished=false;
			transRowDataValue();
			//DWRHandle.getResult(currentPage,allSize, paramMap, pagingCallBackHandle);				
		}
		
	}
	catch(e){
		alert("error:"+e.message);
	}
	
}
function resetPaging(){
	var currentPage = window.parent.currentPage;
	var allSize = window.parent.allSize;
	DWRUtil.setValue("currentPage",currentPage);
	DWRUtil.setValue("allSize",allSize);
	
}
function resetQueryDiv(queryKey){
	var query_keys = queryKey.split(";");
	for(var i=0;i<query_keys.length-1;i++){
		var q = query_keys[i].split("|");
		if(q.length > 1)
			DWRUtil.setValue(q[0],q[1]);
	}
}
function resetParent(){
	window.parent.queryKey = "";
	window.parent.currentPage = "";
	window.parent.allSize = "";
	window.parent.rownum = -1;
}
function saveQueryKey(v){
	window.parent.queryKey = v;
}
function savePage(currentPage,allSize){
	window.parent.currentPage = currentPage;
	window.parent.allSize = allSize;
}
function isSpeValue(str){
	var reg = /['!@#$&*()|{}?^"%\"<>·]+/;
	return reg.test(str);
}
function BASEisNotFloat(theFloat)    
{    
	// 判断是否为浮点数
	len=theFloat.length;    
	dotNum=0;    
	if (len==0)
	{
		return true;    
	}   
	for(var i=0;i<len;i++)
	{    
		oneNum=theFloat.substring(i,i+1);    
		if (oneNum==".")    
			dotNum++;    
		if (((oneNum<"0" || oneNum>"9") && oneNum!=".") || dotNum>1)    
			return true;    
    }    
    if (len>1 && theFloat.substring(0,1)=="0")
    {    
	    if (theFloat.substring(1,2)!=".")    
	      return true;    
    }    
    return false;
}

function queryPre(){
	
	var param_map={};
	var _str="";
	for(var i=0;i<QUERY_KEY.length;i++){
		var value = DWRUtil.getValue(QUERY_KEY[i]);
		if(isSpeValue(value)){
			alert("查询条件不能包含特殊字符！");
			DWRUtil.setValue(QUERY_KEY[i],"");
			queryFlag = false;
			return false;
		}
		param_map[QUERY_KEY[i]] = value;
		_str+=QUERY_KEY[i]+"|"+value+";";
	}
	var backFlag =document.getElementById("backFlag");
	if(backFlag!=null)
		saveQueryKey(_str);
	if(QUERY_ACTION == null){
		alert("DWR Action is null.");
		return false;
	}
	else{
		if(QUERY_ACTION.toLowerCase().indexOf("struts.") > -1 && QUERY_ACTION.toLowerCase().indexOf(_PA_NM) <0){
				QUERY_ACTION =_PA_NM + QUERY_ACTION;
		}
		param_map["QUERY_ACTION"] = QUERY_ACTION;
	}
	return param_map;
}


function query() {
	try{
		//add by zero in 2011-03-16
		window.parent.currentPage = "1";
		//transRowDataValue();
		if(checkQuery()){
			var paramMap = queryPre();
			if(paramMap)
			{
				_paramMap =paramMap;
				//showQueryResult();
				var backFlag = DWRUtil.getValue("backFlag");
				if(backFlag !=null && backFlag == "1"){
					DWRUtil.setValue("backFlag","");
					window.parent.rownum = -1;
				}
				loadFinished=false;
				transRowDataValue();
				//DWRHandle.getResult(1,0, paramMap, pagingCallBackHandle);
			}
		}
	}
	catch(e){
		alert("error:"+e.message);
	}
}
function zeroPaging(){
	DWRUtil.setValue("currentPage",1);
	DWRUtil.setValue("allPages",1);
	DWRUtil.removeAllOptions("changePageTo");
	DWRUtil.setValue("allSize",0);
}

function showResultZoneMsg(msg){
	DWRUtil.removeAllRows("resultZone");
	var tab = $("#resultZone");
	var cl =1;
	if(cell_property[0] != "null"){
		cl = cell_property.length;
	}else cl = cell_property.length-1;
	
	var tr="<tr><td colspan='"+cl+"'><p align='center'>"+msg+"</p></td></tr>";
	tab.append(tr);
}

function pagingCallBackHandle(data) {
	
	DWRUtil.removeAllRows("resultZone");
	var allSize = data.allSize;
	if(data.error != ""){
		zeroPaging();
		alert(data.error);
		return;
	}
	if(allSize == 0){
		showQueryResult();
		zeroPaging();
		showResultZoneMsg("查询不到数据！");		
		return;
	}
	var currentPage = data.currentPage;
	
	var pageSize = data.pageSize;
	initPaging(currentPage,allSize,pageSize);
	
	initResultZone(data.resultList);
	queryResult = data.resultList;
	var backFlag =document.getElementById("backFlag");
	if(backFlag!=null)
		savePage(currentPage,allSize);
	if(window.parent.rownum != null && window.parent.rownum >-1 && backFlag!=null)
		setRowNumChecked(window.parent.rownum);
	
	loadFinished = true;
		
	showQueryResult();
}

function queryAfterTrans() {
	if(reloadFlag){
		var currentPage = window.parent.currentPage;
		//alert(currentPage);
		var allSize = window.parent.allSize;
		if(currentPage == "")currentPage = 1;
		if(allSize == "")allSize = 0;	
		DWRHandle.getResult(currentPage,allSize, _paramMap, pagingCallBackHandle);
	}else {
		reloadFlag=false;
		DWRHandle.getResult(1,0, _paramMap, pagingCallBackHandle);
	}	
}
function callBackTranslate(data){
	i=i+1;
	for(var p in data){
		transResultArray[p] = data[p];
	}
	//alert(i);
	if(i==translateArray.length){
		queryAfterTrans();	
		i=0*1;
	}
}
//转换
function transRowDataValue(){

	for(var i=0;i<cell_property.length;i++){
		var cel = cell_property[i];
		if(cel.indexOf(":") > -1){
			translateArray.push(cel);
			_transFlag[cel] = true;
		}
	}
	if(translateArray.length > 0){
		for(var i=0;i<translateArray.length;i++){
			DWRTranslate.getTransValue(translateArray[i],callBackTranslate);
		}
	}else{
		queryAfterTrans();
	}
}
function initResultZone(resultList){

	
	for(var i=0;i<resultList.length;i++){
		addRowData(resultList[i],i);
	}
	$("#resultZone tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
}

function addRowData(rowData,i){
	
	var tab = $("#resultZone");
	var tr="";
	if(i%2 == 1){
		tr+="<tr class=\"alt\"";
	}else{
		tr+="<tr";
	}
	// onclick=\"try{this.getElementsByTagName('input')[0].checked=true;}catch(e){}\"
	if(cell_property[0] != "null"){
		if(radioClick)
		{
			tr+=" onclick=\"try{this.getElementsByTagName('input')[0].checked=true;}catch(e){}\">";
			var td0="<td align='center'><input type='radio' value='"+rowData[cell_property[0]]+"' onclick='onRadioClick("+i+")' name='check_id'/></td>";
			tr += td0;
		}else{
			tr+=">";
			var td0="<td align='center'><input type='radio' value='"+rowData[cell_property[0]]+"' name='check_id'/></td>";
			tr += td0;
		}
	}else{
		tr+=">";
	}
	for(var i=1;i<cell_property.length;i++){
		if(_transFlag[cell_property[i]] == true){
			var vtp = cell_property[i].split(":")[1];
			var _value = transResultArray[vtp+"."+rowData[vtp]];
			if(_value == undefined){
				_value = rowData[vtp];
			}
			var td = "<td>"+_value+"</td>";
			tr += td;
		}
		else{
			var td = "<td>"+rowData[cell_property[i]]+"</td>";
			tr += td;
		}
	}
	tr += "</tr>";
	tab.append(tr);
}

function initPaging(currentPage,allSize,pageSize){
	DWRUtil.setValue("currentPage",currentPage);
	var allPages = 0;
	var yu = parseInt(allSize)%parseInt(pageSize);
	if(yu == 0){
		allPages = parseInt(allSize/pageSize);
	}
	else{
		allPages = parseInt(allSize/pageSize)+1;
	}
	DWRUtil.setValue("allPages",allPages);
	DWRUtil.setValue("allSize",allSize);
	
	if(currentPage == 1 || DWRUtil.getValue("backFlag") != null){
		DWRUtil.removeAllOptions("changePageTo");
		var cpToArray = new Array();
		for(var p=1;p<=allPages;p++){
			cpToArray.push(p);
		}
		DWRUtil.addOptions("changePageTo", cpToArray); 
	}
	document.getElementById("changePageTo").options[parseInt(currentPage)-1].selected=true;
}
function firstPage(){
	resetRownum();
	var currentPage = DWRUtil.getValue("currentPage");
	var allSize = DWRUtil.getValue("allSize");
	if(currentPage == 1) {
		return;
	}else{
		DWRHandle.getResult(1,allSize, _paramMap, pagingCallBackHandle);
		//document.getElementById("currentPage").innerText = 1;
	}
}
function lastPage(){
	resetRownum();
	var currentPage = parseInt(DWRUtil.getValue("currentPage"));
	var allSize = DWRUtil.getValue("allSize");
	if(currentPage == allPages) {
		return;
	}else{
		var allPages = DWRUtil.getValue("allPages");
		DWRHandle.getResult(allPages,allSize, _paramMap, pagingCallBackHandle);
		//document.getElementById("currentPage").innerText = allPages;
	}
}

function prePage(){
	resetRownum();
	var currentPage = parseInt(DWRUtil.getValue("currentPage"));
	var allSize = DWRUtil.getValue("allSize");
	if(currentPage == 1) {
		return;
	}else{
		DWRHandle.getResult(currentPage-1,allSize, _paramMap, pagingCallBackHandle);
		//document.getElementById("currentPage").innerText = currentPage - 1;
	}
}

function nextPage(){
	resetRownum();
	var allPages = DWRUtil.getValue("allPages");
	var currentPage = DWRUtil.getValue("currentPage");
	var allSize = DWRUtil.getValue("allSize");
	if(currentPage == allPages){
		return;
	}else{
		DWRHandle.getResult(parseInt(currentPage) + 1,allSize, _paramMap, pagingCallBackHandle);
		//document.getElementById("currentPage").innerText = parseInt(currentPage) + 1;
	}
}

function resetRownum(){
	var backFlag =document.getElementById("backFlag");
	if(backFlag!=null)
		window.parent.rownum = -1;
}

function goToPage(){
	resetRownum();
	var currentPage = DWRUtil.getValue("changePageTo");//document.getElementById("changePageTo").value;
	var _currentPage = DWRUtil.getValue("currentPage");
	var allSize = DWRUtil.getValue("allSize");
	if(currentPage == _currentPage) {
		return;
	}else{
		DWRHandle.getResult(currentPage, allSize,_paramMap, pagingCallBackHandle);
		//document.getElementById("currentPage").innerText = 1;
	}
}

function getRowData(i,cel_p){
	return queryResult[i][cel_p];	
}
function getCheckRowNum(){
	var cbs = document.getElementsByName("check_id")
	var rownum=-1 ;
	for(var i=0;i<cbs.length;i++){
		if(cbs[i].checked==true){
			rownum = i;
		}
	}
	return rownum;
}
function setRowNumChecked(i){
	if(i<0)return;
	var cbs = document.getElementsByName("check_id");
	if(cbs == null) return;
	if(cbs.length <=i) return;
	else cbs[i].checked = true;
}
function getCheckedRowData(cel_p){
	var rownum = getCheckRowNum();
	if(rownum == -1 ){
		alert("请选择一条记录！");
		return false;
	}
	return getRowData(rownum,cel_p);
}
function saveCheckRowNum(rn){
	var r = -1;
	if(rn == null){
		r = getCheckRowNum();
	}else{
		r = rn;
	}
	window.parent.rownum=r;
}

